package com.ss.baseinfo.web;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.ss.baseinfo.pojo.CollegeInfo;
import com.ss.baseinfo.pojo.ProfessionalInfo;
import com.ss.baseinfo.service.CollegeService;
import com.ss.baseinfo.service.ProfessionalService;
import com.ss.common.util.JsonUtil;
import com.ss.common.util.MyToolUtil;

/**
 * 专业管理
 * @author Administrator
 *
 */
@Controller
@RequestMapping("professional/")
public class ProfessionalController {
	@Autowired
	private ProfessionalService service;
	@Autowired
	private CollegeService collegeService;
	//新增
	@RequestMapping("/save.do")
	public void save(HttpServletRequest request, 
	           HttpServletResponse response) throws Exception{
		Map<String, Object> mapResult=new HashMap<String, Object>();
		String action=MyToolUtil.convertNull(request.getParameter("action"));
		String id=MyToolUtil.convertNull(request.getParameter("id"));
		String name=MyToolUtil.convertNull(request.getParameter("name"));
		String collegeId=MyToolUtil.convertNull(request.getParameter("collegeId"));
		
		ProfessionalInfo info = new ProfessionalInfo();
		info.setName(name);
		info.setCollege_id(collegeId);
		String msg="";	
		if("add".equals(action)){
			service.add(info);
			msg="保存成功";
		}else{
			info.setId(id);
			boolean ret=service.update(info);
			if(ret){
				msg="保存成功";
			}else{
				msg="保存失败";
			}
		}
		mapResult.put("msg", msg);
		JsonUtil.outputJson(mapResult, response);
	}
	//调整到编辑页面
	@RequestMapping("/toEdit.do")
	public ModelAndView toEdit(HttpServletRequest request, 
	           HttpServletResponse response) throws Exception{
		Map<String, Object> mapResult=new HashMap<String, Object>();
		String id=MyToolUtil.convertNull(request.getParameter("id"));
		if(!id.equals("")){
			ProfessionalInfo info = new ProfessionalInfo();
			info=service.getOneById(id);
			mapResult.put("info", info);
		}
		//获取学院信息
		List<CollegeInfo> listCollege=collegeService.query();
		mapResult.put("listCollege", listCollege);
		return new ModelAndView("/baseinfo/professionalEdit",mapResult);
	}
	//保存
	@RequestMapping("/query.do")
	public ModelAndView query(HttpServletRequest request, 
	           HttpServletResponse response) throws Exception{
		Map<String, Object> mapResult=new HashMap<String, Object>();
		String collegeId=MyToolUtil.convertNull(request.getParameter("collegeId"));
		ProfessionalInfo info = new ProfessionalInfo();
		info.setCollege_id(collegeId);
		List<ProfessionalInfo> list= service.query(info);
		mapResult.put("list", list);
		//获取学院信息
		List<CollegeInfo> listCollege=collegeService.query();
		mapResult.put("listCollege", listCollege);
		return new ModelAndView("baseinfo/professionalManage",mapResult);
	}
	/**
	 * 查询专业信息,json
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping("/queryjson.do")
	public void queryjson(HttpServletRequest request, 
	           HttpServletResponse response) throws Exception{
		Map<String, Object> mapResult=new HashMap<String, Object>();
		String collegeId=MyToolUtil.convertNull(request.getParameter("collegeId"));
		ProfessionalInfo info = new ProfessionalInfo();
		info.setCollege_id(collegeId);
		List<ProfessionalInfo> list= service.query(info);
		mapResult.put("list", list);
		System.out.println(mapResult);
		JsonUtil.outputJson(mapResult, response);
	}
}
